using System;
using System.IO;
using System.Security.Permissions;

namespace Antix
{
    /// <summary>
    /// Help class for IO functions
    /// </summary>
    [FileIOPermission(SecurityAction.Demand,Unrestricted=true)]
    public class IOFunctions
    {
        #region Path

        /// <summary>
        /// Create a path.
        /// 
        /// </summary>
        /// <param name="path">Path to create</param>
        /// <returns>true if new path created, false if path exists already</returns>
        public static bool CreatePath(string path)
        {
            try
            {
                //find the last folder
                int intFolderStart = path.LastIndexOf("\\");

                if (intFolderStart >= 0)
                {
                    //create sub folders by recursion
                    CreatePath(path.Substring(0, intFolderStart));
                    if (!Directory.Exists(path))
                    {
                        //create folder
                        Directory.CreateDirectory(path);

                        return true;
                    }
                }

                return false;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// Ensure that a path ends with a back slash
        /// </summary>
        /// <param name="path">Path to format</param>
        /// <returns>Formatted path</returns>
        public static string FormatPath(string path)
        {
            if (!path.EndsWith("\\")) { path += "\\"; }
            return Path.GetFullPath(path);
        }

        /// <summary>
        /// Delete a path, if it exsists and there is permission, doesn't raise any error
        /// </summary>
        /// <param name="path">Path to delete</param>
        public static void DeletePath(string path)
        {
            DeletePath(path, false);
        }

        /// <summary>
        /// Delete a path, if it exsists and there is permission, doesn't raise any error
        /// </summary>
        /// <param name="path">Path to delete</param>
        /// <param name="contentsOnly">Delete the contents only</param>
        public static void DeletePath(string path, bool contentsOnly)
        {
            try
            {
                if (contentsOnly)
                {
                    foreach (string file in Directory.GetFiles(path))
                    {
                        File.Delete(file);
                    }
                }
                else { Directory.Delete(path, true); }
            }
            catch (Exception) { }
        }

        #endregion

        #region TextFile

        /// <summary>
        /// Read the contents of a text file
        /// </summary>
        /// <param name="filePath">File to read</param>
        /// <returns>Contents of the file</returns>
        public static string ReadTextFile(string filePath)
        {
            StreamReader file = File.OpenText(filePath);
            try
            {
                return file.ReadToEnd();
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                file.Close();
            }
        }

        /// <summary>
        /// Create a text file and path if necessary, then write the contents and close
        /// </summary>
        /// <param name="filePath">File to create</param>
        /// <param name="contents">Contents of the file</param>
        public static void CreateTextFile(string filePath, string contents)
        {
            // Check path exists
            string path = filePath.Substring(0, filePath.LastIndexOf("\\"));
            if (!Directory.Exists(path)) { IOFunctions.CreatePath(path); }

            StreamWriter file = File.CreateText(filePath);
            try
            {
                file.Write(contents);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                file.Close();
            }
        }

        #endregion

        #region ToByteArray

        /// <summary>
        /// Convert a stream into a byte array
        /// </summary>
        /// <param name="stream">Stream to convert</param>
        /// <returns>Converted stream</returns>
        public static byte[] ToByteArray(Stream stream)
        {
            byte[] data = new byte[stream.Length];
            stream.Read(data, 0, (int)stream.Length);

            return data;
        }

        #endregion
    }
}